/**
 * @author  李珂<likenamehaojie@hotmail.com>
 * @data    20170411
 * vuex types
 */
import * as types from './types'
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
Array.prototype.continer = function (datas) {
    for (let data of this) {
        if (data.title == datas.title) {
            return true;
        }
    }
    return false;
}
export default new Vuex.Store({
    strict: process.env.NODE_ENV !== 'production',//在开发环境开启严格模式
    state: {
        user: {},
        username:'',
        token: null,
        titles: [{title: "首页", index: '/welcome'}],
        curentActivityIndex: 0,
        currentActivityMenue: [],
        currentUser: {},
        categoryID: '',
        allLoadIng:false,
        categoryAdd: '',
        categoryDelete: '',
        minSize:true,

    },
    mutations: {
        [types.LOGIN]: (state, data) => {
            localStorage.token = data;
            state.token = data;
        },
        [types.CHANGE_NAME]: (state, data) => {
            state.username = data;
        },
        [types.LOGIN_REMOVE_TITLES]: (state) => {
            state.titles = [{title: "首页", index: '/welcome'}];
        },
        [types.LOGOUT]: (state) => {
            localStorage.removeItem('token');
            state.token = null;
            // state.titles=[{title: "首页", index: '/welcome'}];
        },
        [types.WIN_SIZE]: (state,data) => {
            state.minSize=!data
        },
        [types.TITLES_ADD]: (state, data) => {

            if (!state.titles.continer(data)) {
                if (state.titles.length > 6) {
                    state.titles.splice(1, 1);

                }
                state.titles.push(data);
            }
        },
        [types.TITLES_ACTIVITY]: (state, index) => {
            console.log(index);
            state.curentActivityIndex = index;

        },
        [types.TITLES_REMOVE]: (state, index) => {
            var arr = [];
            for (var i = 0; i < state.titles.length; i++) {
                if (i != index) {
                    arr.push(state.titles[i]);
                }
            }
            ;
            state.titles = arr;
        },
        [types.REF_CURRENT_MENU]: (state, data) => {
            state.currentActivityMenue.splice(0, state.currentActivityMenue.length)
            for (let item of data) {
                state.currentActivityMenue.push(item);
            }
        },
        [types.CURRENT_USER_ADD]: (state, data) => {
            state.currentUser = data
        },
        [types.CHANGE_INDEX]: (state, data) => {
            state.titles[state.curentActivityIndex].index = data
        },
        [types.CATEGORY_ID_CHANGE]: (state, data) => {
            state.categoryID = data
        },
        [types.CATEGORY_ADD]: (state, data) => {
            state.categoryAdd = data
        },
        [types.CATEGORY_DELETE]: (state, data) => {
            state.categoryDelete = data
        },
        [types.ALL_LOADING]: (state, data) => {
            state.allLoadIng = data;
        },




    }
})
